package com.jiuyi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiuyi.common.R;
import com.jiuyi.entity.Category;
import com.jiuyi.entity.Dish;
import com.jiuyi.entity.Setmeal;
import com.jiuyi.exception.SystemException;
import com.jiuyi.mapper.CategoryMapper;

import com.jiuyi.mapper.DishMapper;
import com.jiuyi.mapper.SetmealMapper;
import com.jiuyi.service.CategoryService;
import com.jiuyi.utils.BeanCopyUtils;
import com.jiuyi.vo.AdminSelectCategoryListVo;
import com.jiuyi.vo.AdminSelectCategoryVo;
import com.jiuyi.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.jiuyi.emum.HttpMsgMenu.*;

/**
 * DATA: 2022/10/19
 * Author: 晨夕
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加分类
     */
    @Transactional
    @Override
    public R addCategory(Category category) {
        //分类名、排序不能为空
        notNull(category);
        //分类不能重复
        LambdaQueryWrapper<Category> query = new LambdaQueryWrapper<>();
        query.eq(Category::getName,category.getName());
        Category categoryExist = categoryMapper.selectOne(query);
        if(!ObjectUtils.isEmpty(categoryExist)){
            throw new SystemException(NAME_IS_EXIST);
        }
        categoryMapper.insert(category);
        return R.success("添加成功");
    }

    /**
     * 查询分类
     */
    @Override
    public R<AdminSelectCategoryVo> selectCategory(int page, int pageSize) {
        List<Category> categories = categoryMapper.selectCategoryList(page, pageSize);
        int count = categoryMapper.selectCategoryCount();
        List<AdminSelectCategoryVo> adminSelectCategoryVos = BeanCopyUtils.copyBeanList(categories, AdminSelectCategoryVo.class);
        PageVo<AdminSelectCategoryVo> pageVo = new PageVo<>();
        pageVo.setRecords(adminSelectCategoryVos);
        pageVo.setTotal(count);
        return R.ok(pageVo);
    }

    /**
     * 修改分类
     */
    @Override
    public R updateCategory(Category category) {
        //分类名、排序不能为空
        notNull(category);
        //分类不能重复
        int count = categoryMapper.selectNameIsExist(category.getName(), category.getId());
        if(count > 0){
            throw new SystemException(NAME_IS_EXIST);
        }
        categoryMapper.updateById(category);
        return R.success("修改成功");
    }
    /**
     * 删除分类
     */
    @Override
    public R deleteCategory(Long ids) {
        //1. 查看分类是否有菜品，有菜品无法删除
        Dish dish = dishMapper.selectCategoryInDish(ids);
        if(!ObjectUtils.isEmpty(dish)){
            throw new SystemException(DISH_EXIST_NOW);
        }
        //2. 查看分类是否有套餐，有套餐无法删除
        Setmeal setmeal = setmealMapper.selectCategoryInSetmeal(ids);
        if(!ObjectUtils.isEmpty(setmeal)){
            throw new SystemException(SETMEAL_EXIST_NOW);
        }
        categoryMapper.deleteById(ids);
        return R.success("删除成功");
    }
    /**
     * 查询分类列表
     */
    @Override
    public R<AdminSelectCategoryListVo> selectCategoryList() {
        List<Category> categories = categoryMapper.selectCategoryLists();
        List<AdminSelectCategoryListVo> adminSelectCategoryListVos = BeanCopyUtils.copyBeanList(categories, AdminSelectCategoryListVo.class);
        return R.ok(adminSelectCategoryListVos);
    }

    /**
     * 不能为空的数据
     */
    public void notNull(Category category){
        if(ObjectUtils.isEmpty(category.getName())){
            throw new SystemException(CATEGORY_NAME_IS_NULL);
        }
        if(ObjectUtils.isEmpty(category.getSort())){
            throw new SystemException(CATETORY_SORT_IS_NULL);
        }
    }
}
